package com.wanban.download.utils

import android.text.TextUtils
import com.wanban.download.model.M3U8
import com.wanban.download.model.Ts
import org.json.JSONArray
import org.json.JSONObject


object CacheM3U8Utils {


    fun getCacheM3U8(tag: String): M3U8? {
        // 继续尝试从SPUtils中获取
        try {
            val cacheM3U8 = spGetM3U8(tag)
            return if (TextUtils.isEmpty(cacheM3U8)) {
                null
            } else {
                val jsonObject = JSONObject(cacheM3U8!!)
                if (!jsonObject.has("url")) {
                    return null
                }
                val m3U8 = M3U8(jsonObject.optString("url"))
                m3U8.isMp4 = jsonObject.optBoolean("isMp4")
                m3U8.totalLength = jsonObject.optLong("totalLength")

                val tsJSONArray = jsonObject.optJSONArray("tsList")
                if (tsJSONArray != null) {
                    val length = tsJSONArray.length()
                    for (index in 0 until length) {
                        val tsJSONObject = tsJSONArray.optJSONObject(index)
                        val ts = Ts()
                        ts.url = tsJSONObject.optString("url")
                        ts.index = tsJSONObject.optInt("index")
                        ts.tsSize = tsJSONObject.optLong("tsSize")
                        ts.complete = tsJSONObject.optBoolean("complete")
                        ts.method = tsJSONObject.optString("method")
                        ts.keyIV = tsJSONObject.optString("keyIV")
                        ts.keyUri = tsJSONObject.optString("keyUri")
                        ts.retryTimes = tsJSONObject.optInt("retryTimes")
                        ts.isRetrying = tsJSONObject.optBoolean("isRetrying")

                        m3U8.addTs(ts)
                    }
                }
                m3U8
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return null
        }
    }

    fun cacheM3U8(tag: String, m3u8: M3U8){
        try {
            // 使用SPUtils存储
            val jsonObject = JSONObject()
            jsonObject.put("url", m3u8.url)
            jsonObject.put("isMp4", m3u8.isMp4)
            jsonObject.put("totalLength", m3u8.totalLength)

            val tsJsonArray = JSONArray()
            m3u8.tsList.forEach { ts ->
                val tsJsonObject = JSONObject()
                tsJsonObject.put("url", ts.url)
                tsJsonObject.put("index", ts.index)
                tsJsonObject.put("tsSize", ts.tsSize)
                tsJsonObject.put("complete", ts.complete)
                tsJsonObject.put("keyIV", ts.keyIV)
                tsJsonObject.put("method", ts.method)
                tsJsonObject.put("keyUri", ts.keyUri)
                tsJsonObject.put("retryTimes", ts.retryTimes)
                tsJsonObject.put("isRetrying", ts.isRetrying)

                tsJsonArray.put(tsJsonObject)
            }

            jsonObject.put("tsList", tsJsonArray)
            val m3u8Json = jsonObject.toString()
            if (!TextUtils.isEmpty(m3u8Json)) {
                spCacheM3U8(tag, m3u8Json)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    fun clearCacheM3U8(tag: String){
        spClear(tag)
    }


    /**
     * --------------------------------  SPUtils  -------------------------------------
     */
    private fun spGetM3U8(tag: String): String? {
        return VideoDownloadSPUtil.getSPUtils().getString(tag, null)
    }

    private fun spCacheM3U8(tag: String, m3u8Str: String){
        // 本地化
        VideoDownloadSPUtil.getSPUtils().edit()?.putString(tag, m3u8Str)?.apply()
    }

    private fun spClear(tag: String){
        VideoDownloadSPUtil.getSPUtils().edit()?.remove(tag)?.apply()
    }

}